<template>
  <div class="sub-filter" v-if="filters && filtersLoading">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
  <!-- 筛选区 -->
  <div class="sub-filter" v-else>
    <div class="item">
      <div class="head">品牌</div>
      <div class="body">
        <a
          :class="{ active: filters.selectBrandId === item.id }"
          href="javascript:"
          v-for="item in filters?.brands"
          :key="item.id"
          @click="
            filters.selectBrandId = item.id;
            updateSelectedFilters();
          "
          >{{ item.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="item in filters?.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}</div>
      <div class="body">
        <a
          :class="{
            active: sub.name === item.selectedFilterName,
          }"
          href="javascript:"
          v-for="sub in item.properties"
          :key="sub.id"
          @click="
            item.selectedFilterName = sub.name;
            updateSelectedFilters();
          "
          >{{ sub.name }}</a
        >
      </div>
    </div>
  </div>
</template>
<script>
import { getSubCategoryFilterById } from "@/api/category";
import { ref } from "vue";
import { onBeforeRouteUpdate, useRoute } from "vue-router";

export default {
  name: "SubFilter",
  setup(props, { emit }) {
    const route = useRoute();
    const { filters, filtersLoading, useData, updateSelectedFilters } =
      usefilter(emit);
    useData(route.params.id);
    return {
      filters,
      updateSelectedFilters,
      filtersLoading,
    };
  },
};
function usefilter(emit) {
  const filters = ref(null);
  // 用于汇总临时存入的数据
  const selectedFilters = {
    // 选中的品牌id
    brandId: null,
    // 选中的其他分类数据
    attrs: [],
  };
  // 用于存储筛选条件的加载状态;
  const filtersLoading = ref(false);
  // 汇总数据 将临时存的数据汇总到一起,(点击筛选分类时触发)
  const updateSelectedFilters = () => {
    // 每次添加都需要先清空之前的分类
    selectedFilters.attrs = [];
    //保存品牌id
    selectedFilters.brandId = filters.value.selectBrandId;
    // 保存选中的其他分类
    filters.value.saleProperties.forEach((item) => {
      // 判断,如果有暂存的数据则push
      if (item.selectedFilterName && item.selectedFilterName !== "全部") {
        selectedFilters.attrs.push({
          groupName: item.name,
          propertyName: item.selectedFilterName,
        });
      }
    });
    // 将汇总的数据传入到父组件;
    emit("onFilterChange", selectedFilters);
  };
  // 初始获取筛选条件
  const useData = (id) => {
    // 更新筛选数据的加载状态;
    filtersLoading.value = true;
    getSubCategoryFilterById(id).then((data) => {
      // 更新筛选数据的加载状态
      filtersLoading.value = false;
      // 给品牌添加"全部"分类,要在数组前添加
      data.result.brands.unshift({ id: null, name: "全部" });
      // 设置默认选中筛选条件
      data.result.selectBrandId = null;
      // 给其他分类添加全部分类选项
      data.result.saleProperties.forEach((key) => {
        key.properties.unshift({ id: null, name: "全部" });
        key.selectedFilterName = "全部";
      });
      // 设置默认选中的筛选条件的名字

      filters.value = data.result;
    });
  };
  // 解决二级路由跳转筛选条件不更新的问题
  onBeforeRouteUpdate((to) => {
    useData(to.params.id);
  });
  return {
    filters,
    useData,
    updateSelectedFilters,
    // 更新筛选数据的加载状态
    filtersLoading,
  };
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
.xtx-skeleton {
  padding: 10px 0;
}
</style>
